package org.dandelion.account;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.dandelion.R;
import org.dandelion.account.util.Account;
import org.dandelion.account.util.DBHelper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class AccountActivity extends AppCompatActivity {
    private static final int AddAccounts = Menu.FIRST;
    private TextView tv_Date, tv2;
    private ListView listView;
    Button btnDate;
    private Button btn2;
    Calendar calendar = Calendar.getInstance(Locale.CHINA);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);

        tv2 = findViewById(R.id.textView2);
        listView = findViewById(R.id.listView);
        btnDate = findViewById(R.id.btn_Date);
        tv_Date = findViewById(R.id.tv_Date);
        btn2 = findViewById(R.id.button2);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String date = year + " - " + month + " - " + day;
        tv_Date.setText(date);
        btn2.setOnClickListener(v -> {
            DBHelper helper = new DBHelper(AccountActivity.this);
            String vdate = tv_Date.getText().toString();
            List<Account> list = helper.query(vdate);
            List<Map<String, String>> mapList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("id", String.valueOf(list.get(i).getId()));
                map.put("type", list.get(i).getType());
                map.put("money", String.valueOf(list.get(i).getMoney()));
                map.put("rb", list.get(i).getRb());
                map.put("info", list.get(i).getInfo());
                map.put("date", list.get(i).getDate());
                mapList.add(map);
            }
            SimpleAdapter adapter = new SimpleAdapter(AccountActivity.this, mapList,
                    R.layout.list, new String[]{"id", "type", "money", "rb", "info", "date"},
                    new int[]{R.id.id, R.id.type, R.id.money, R.id.rb, R.id.info, R.id.date});
            listView.setAdapter(adapter);
            listView.setOnItemClickListener((parent, view, position, id) -> {
                Map<String, String> map =
                        (Map<String, String>) listView.getItemAtPosition(position);
                String flag = map.get("id");
                AlertDialog.Builder adBuilder = new AlertDialog.Builder(
                        AccountActivity.this);
                adBuilder.setMessage("确认删除本记录？").setPositiveButton("确认",
                        (dialog, which) -> {
                            helper.delete(Integer.parseInt(flag));
                            for (int i = 0; i < mapList.size(); i++) {
                                Map<String, String> nmap = mapList.get(i);
                                if (nmap.get("id").equals(flag)) {
                                    mapList.remove(i);
                                    break;
                                }
                            }
                            SimpleAdapter nadapter = new SimpleAdapter(
                                    AccountActivity.this, mapList, R.layout.list,
                                    new String[]{"id", "type", "money", "rb", "info", "date"},
                                    new int[]{R.id.id, R.id.type, R.id.money, R.id.rb,
                                            R.id.info, R.id.date}
                            );

                            listView.setAdapter(nadapter);
                            double balance = helper.getBalance(vdate);
                            tv2.setText(("以下为本日的账单\n" + "本日支出收入之和为：" + balance));
                        }).setNegativeButton("取消", (dialog, which) -> {
                        }
                );
                adBuilder.show();
            });

            double balance = helper.getBalance(vdate);
            tv2.setText(("以下为本日的账单\n" + "本日支出收入之和为：" + balance));
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, AddAccounts, 100, "选择记账类型");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        final String[] items = new String[]{"收入", "支出"};
        AlertDialog.Builder builder = new AlertDialog.Builder(AccountActivity.this);
        builder.setTitle("请选择记账类型：");
        builder.setItems(items, (dialog, which) -> {
            Intent intent = new Intent();
            intent.setClass(AccountActivity.this, AccInfoActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("type", items[which]);
            bundle.putString("dates", tv_Date.getText().toString());
            intent.putExtras(bundle);
            startActivity(intent);
        });
        builder.create().show();
        return super.onOptionsItemSelected(item);
    }

    public static void showDatePickerDialog(Activity activity, int themeResId, final TextView tv,
                                            Calendar calendar) {
        new DatePickerDialog(activity, themeResId, (view, year, monthOfYear, dayOfMonth)
                -> tv.setText((year + " - " + (monthOfYear + 1) + " - " + dayOfMonth))
                // 设置初始日期
                , calendar.get(Calendar.YEAR)
                , calendar.get(Calendar.MONTH)
                , calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    public void DateClick(View view) {
        showDatePickerDialog(this, 4, tv_Date, calendar);
    }
}